package script.dj;

import httputils.OkHttpUtils;
//import cn.hutool.core.codec.Base64;
//import cn.hutool.core.util.URLUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/26 10:43
 * @since JDK 1.8
 *
 * 内容添加脚本
 */
public class AddContentScript {

    public static void main(String[] args) {
        String url="http://127.0.0.1:8080/cmsmanager/content";
        String token="eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzeXN0ZW0iLCJjcmVhdGVkIjoxNjI3Mjc4NjQ5OTE0LCJ1c2VyU291cmNlIjoiYWRtaW4iLCJleHAiOjE2MjgxNDI2NDl9.qWO67XrGkh6wgSxaXc1T46_ofkTz1VlOkamKHCQN2gBSqYMG-xx5qq_X53DKZGHd1IpGIpIk3T3qPC30uuBEaQ";
        Map<String, Object> objectMap=addDJW("这是标题121","这是内容","这是作者","这是摘要",2676);
        dataRequest(url,token,objectMap);
    }

    public static Map<String, Object> addDJW(String title,String content,String author,String desc,Integer channelId){
        return addContent(title,author,content,desc,1422,"通用",channelId);
    }


    public static Map<String, Object> addContent(String title,String author,String content,String description,
    Integer modelId,String modelName,Integer channelId){
        return addParam(content,modelId,modelName,channelId,title,author,description,"","","");
    }


//    //内容操作
//    public static String content(String content){
//       // String urlEncode= URLUtil.encode(content); //url加码
//      //  String base64Encode= Base64.encode(urlEncode);    //base64加密
//       // return base64Encode;
//    }

    //发送请求
    public static void dataRequest(String url,String token, Map<String, Object> objectMap){
        String string= OkHttpUtils.builder().url(url)
                // 有参数的话添加参数，可多个
                // 也可以添加多个
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("Connection","keep-alive")
                .addHeader("JEECMS-Auth-Token",token)
                .post(objectMap)
                // 可选择是同步请求还是异步请求
                //.async();
                .sync();
        System.out.println(string);
    }

    //内容格式创建
    public static Map<String, Object> addParam(String content,Integer modelId,String modelName,Integer channelId,String title,String author,
                                               String description,String outlink,String sourceLink,String sourceName){
        Map<String, Object> stringObjectMap=new HashMap<>();
        //内容
        Map<String, Object> contentObj=new HashMap<>();
        //标题
        Map<String, Object> titleObj = new HashMap<>();
        //外链
        Map<String, Object> outObj=new HashMap<>();
        //来源
        Map<String, Object> contentSourceIdMap=new HashMap<>();

        //内容处理
      //  content=content(content);
        contentObj.put("content",content);
        stringObjectMap.put("json",contentObj);


        stringObjectMap.put("modelId",modelId);
        stringObjectMap.put("modelName",modelName);
        stringObjectMap.put("channelId",channelId);

        //内容处理
        titleObj.put("titleColor","#333333");
        titleObj.put("titleIsBold",true);
        titleObj.put("title",title);
        stringObjectMap.put("title",titleObj);

        stringObjectMap.put("author",author);//作者
        stringObjectMap.put("description",description);//摘要

        outObj.put("isNewTarget",false);
        outObj.put("outLink",outlink);
        stringObjectMap.put("outLink",outObj);

        contentSourceIdMap.put("sourceLink",sourceLink);
        contentSourceIdMap.put("sourceName",sourceName);
        stringObjectMap.put("contentSourceId",contentSourceIdMap);
        stringObjectMap.put("releaseTime",nowDate());
        stringObjectMap.put("type",2);
        stringObjectMap.put("tplMobile","");
        stringObjectMap.put("tplPc","");

        return stringObjectMap;

    }

    //创建当前时间
    public static String nowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }






}
